{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def findPairs(self, nums: [int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n <= 1:\n",
    "            return 0\n",
    "        \n",
    "        nums.sort()\n",
    "        left = 0\n",
    "        right = 1\n",
    "        cnt = 0\n",
    "        is_unique = {}\n",
    "        \n",
    "        while right < n:\n",
    "            diff = nums[right] - nums[left]\n",
    "            if diff == k:\n",
    "                if nums[right] not in is_unique:\n",
    "                    cnt += 1\n",
    "                    is_unique[nums[right]] = True\n",
    "                left += 1\n",
    "                right += 1\n",
    "            elif diff > k:\n",
    "                left += 1\n",
    "            elif diff < k:\n",
    "                right += 1\n",
    "            if left == right:\n",
    "                right += 1\n",
    "                \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = Solution()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.findPairs([1,1,1,1,1], 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
